<?php

class Admin_Model_Zipcode {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_Zipcode();
    }

    /**
     * 
     * @return type
     */
    public function fetchAll() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function createNewZip($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateZip($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getZipById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ?", $id);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * This will give all zips in array format, for selected id.
     * @param type $id
     * @param type $formated
     * @return type array
     */
    public function getZipArray($id, $formated) {
        $zipById = $this->getZipById($id);

        $array = array(
            'id' => $zipById[0]['id'],
            'code' => $zipById[0]['code'],
            'city_id' => $zipById[0]['city_id']
        );
        if ($formated) {
            $array = array('zipcode' => $array);
        }
        return $array;
    }

    /**
     * 
     * @param type $formated
     * @return type
     */
    public function getAllZipArray($formated) {
        $fetchAll = $this->fetchAll();
        $array = array();
        foreach ($fetchAll as $zip) {
            $local = array(
                'id' => $zip->id,
                'code' => $zip->code,
                'city_id' => $zip->city_id,
            );
            array_push($array, $local);
        }
        if ($formated) {
            $array = array("zipcode" => $array);
        }
        return $array;
    }

    /**
     * 
     * @param type $id
     * @param type $formated
     * @return type
     */
    public function getZipWithCityArray($id, $formated = false) {
        $cityDb = new Admin_Model_City();
        $zipArray = $this->getZipArray($id, false);
        $cityStateArray = $cityDb->getCityWithStateArray($zipArray['city_id'], false);
        $array = array('zipcode' => $zipArray, 'city' => $cityStateArray[0], 'state' => $cityStateArray[1]);
        return $array;
    }

    /**
     * 
     * @param type $formated
     * @return array
     */
    public function getAllZipWithCityArray($formated = true) {
        $fetchAll = $this->fetchAll();
        $array = array();
        foreach ($fetchAll as $zip) {
            $zipWithCityArray = $this->getZipWithCityArray($zip['id'], false);
            array_push($array, $zipWithCityArray);
        }
        return $array;
    }

    /**
     * 
     * @param type $zipCode
     * @return boolean
     */
    public function isExists($zipCode) {
        $zips = $this->getAllZipArray(false);
        foreach ($zips as $zip) {
            if ($zip['code'] == $zipCode) {
                return true;
            }
        }
        return false;
    }

}

